home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: What's so different about 2-D arrays???
- Date: 31 Dec 1995 19:18:51 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4c799b$q9d@umbc9.umbc.edu>
- References: <820451938.20697@fredblog.demon.co.uk>
- NNTP-Posting-Host: f-umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Mark Winfield <mark@fredblog.demon.co.uk> wrote:
- |> I have been teaching myself 'C' over the last month. Just before Xmas
- |> I thought that I had it worked out and that all I had to do was
- |> practice. I decided to write a program that would record chess games,
- |> it would allow move branching as well. I decided upon this because it
- |> should use everything I have learnt except DOS calls.
- |> Now for the problem,
- |>
- |> //My own chess recorder program
-
- Illegal comment! This is comp.lang.c not comp.lang.c++.
-
- |> #include <stdio.h>
- |> #include <ctype.h>
-
- Why do you need <ctype.h>?
-
- |> /*Prototypes*/
- |> void setup(char pos[8][8]);
- |>
- |> void main()
-
- Please read the FAQ. This will show you why 'int main (void)' is the correct
- declaration for your particular program.
-
- |> {
- |> char pos[8][8];
- |>
- |> setup(pos);
- |>
- |>
- |> printf("\nWell Done!!");
-
- A simple 'return (0);' would go here nicely.
-
- |> }
- |>
- |> void setup(char pos[8][8])
-
- What exactly do you think an array is and how do you believe it is
- supposed to be indexed? In C arrays start at 0 so an array with
- 8 elements is indexed from 0-7 not 1-8. This is true for each and
- every dimension of a multi-dimensional array.
-
- |> {
- |> int x,y;
- |>
- |> pos[1][8]=pos[8][8]='r';
- |> pos[2][8]=pos[7][8]='n';
- |> pos[3][8]=pos[6][8]='b';
- |>
- |> pos[1][1]=pos[8][1]='R';
- |> pos[2][1]=pos[7][1]='N';
- |> pos[3][1]=pos[6][1]='B';
-
- In each and every one of these 6 statements you explicitly access data
- beyond the array. Especially for pos[8][8] which is way outside your
- legal range!
-
- |> for(x=1;x<9;x++){
- |> pos[x][7]='a';
- |> pos[x][2]='A';
-
- Here you go from 1-8 so when 'x == 8' you're in trouble.
-
- |> for(y=3;y<7;y++)
- |> pos[x][y]=' ';
- |> }
- |>
- |> pos[4][8]='q';
- |> pos[5][8]='k';
-
- More explicit accesses which are illegal.
-
- |> pos[4][1]='Q';
- |> pos[5][1]='K';
- |> }
- |>
- |> This program does not work with a '.c' or '.cpp' extension. If I run
- |> the program from dos I get well done and then something about NULL
- |> pointers and the program hangs, so I have to reset. And if I run the
- |> program from within the TURBO C++ enviroment the program hangs after
- |> printing well done.
-
- Are you saying you get problems about NULL pointers at run time or
- compile time?
-
- |> I have written a similar program since, as a test, which uses a 1-D
- |> array with no problems. Can someone help me with this problem.
-
- Show us the 1D example for comparison...Maybe you just got lucky?
- However, it's always preferable to post the smallest ANSI C example
- which produces the problem which you have come close to doing.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-